<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bigdata_model extends Model
{	
	function Campaign_model()
	{
		parent::Model();
	}
	
	function optpriority_get_all()
	{
		$data =array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL bigdata_admin_optpriority_get_all();") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
        	$row['username'] = ($row['userid'] == 0) ? 'All user' : $row['username'];
        	$row['contact_name'] = ($row['userid'] == 0) ? 'All user' : $row['contact_name'];
        	$data[] = $row;
        }
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
        return $data;
	}
	
	function optpriority_action($action, $uid, $val)
	{
		$iconn = $this->db->conn_id;
		echo $action . ' - ' . $uid . ' - ' . $val;
		mysqli_multi_query($iconn, "CALL bigdata_admin_optpriority_action('$action', '$uid', '$val');") or show_custom_error(mysqli_error($iconn));
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
	}
	
	function get_all_group()
	{
		$data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_all_group();") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
	function suser_search_user($groupId, $stype, $keyword, $keyword_en, $cPage, $num)
	{		
		$data = array();
		$data['lstUser'] = array();
		$data['iCount'] = 0;
		$data['pCount'] = 0;
		$start = ($cPage - 1) *  $num;
		mysqli_multi_query($this->db->conn_id, "CALL bigdata_admin_search_user('$groupId', '$stype', '$keyword', '$keyword_en','$start', '$num');") or show_custom_error(mysqli_error($this->db->conn_id));
		$result = mysqli_store_result($this->db->conn_id);
		while($row = mysqli_fetch_array($result))
		{
			$data['lstUser'][] = $row;
		}
		mysqli_free_result($result);
		
		mysqli_next_result($this->db->conn_id);
		$result = mysqli_store_result($this->db->conn_id);
		$row = mysqli_fetch_array($result);
		$data['iCount'] = $row['num'];
		$data['pCount'] = ($data['iCount'] > $num) ? ceil($data['iCount']/$num) : 1;
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
	
	function get_list_user($permitId, $groupId, $cPage, $numItem)
	{				
		$data = array();
		$data['lstUser'] = array();
		$data['iCount'] = 0;
		$data['pCount'] = 0;
		$start = ($cPage - 1) * $numItem;
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_suser_get_list_user('$permitId', '$groupId', '$start', '$numItem');") or show_custom_error(mysqli_errno($iconn));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data['lstUser'][] = $row;
		}
		mysqli_free_result($result);
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_array($result);
		$data['iCount'] = $row['num'];
		$data['pCount'] = ($data['iCount'] > $numItem) ? ceil($data['iCount']/$numItem) : 1;
		
		while(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
}
?>